﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using Microsoft.Win32;
using System.Configuration;

namespace ZG_member
{
    public partial class ZhuCeForm : Form
    {
        public ZhuCeForm()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
        }
        public event EventHandler Logind;
        private bool isLogined;
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            Process.GetCurrentProcess().Kill();
        }
       public string mima ;      
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                if (mima.Equals(textBox2.Text.TrimEnd()))
                {
                    
                    Writer();
                    MessageBox.Show("注册成功");
                    RegistryKey hklm = Registry.LocalMachine;
                    RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
                    RegistryKey ddd = software.CreateSubKey("ruanjianzhuce");
                    string date = DateTime.Now.ToString();
                    ddd.SetValue("zhucechenggong", date);
                    

                    this.isLogined = true;
                    this.Close();

                    if (Logind != null)
                    {
                        Logind(sender, e);
                    }
                    
                }
                else
                {
                    MessageBox.Show("注册码输入错误,请重新输入！");
                }

            }
            else
            {
                MessageBox.Show("请生成注册码", "注册提示");
            }

        }
        public void Writer()
        {
           
            string xx;
            xx = "True";
            StreamWriter sw = new StreamWriter("Clent.ini", false, Encoding.GetEncoding("gb2312"));
            sw.Flush();
            sw.Write(xx);
            sw.Close();
        }

        private void ZhuCeForm_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            System.Management.ManagementClass mc = new ManagementClass("Win32_Processor");
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            string strCpuID = null;
            foreach (System.Management.ManagementObject mo in moc)
            {
                strCpuID = mo.Properties["ProcessorID"].Value.ToString();
                char[] arr = strCpuID.ToCharArray();
                Array.Reverse(arr);
                string CpuID = new string(arr);





                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] data = System.Text.Encoding.Default.GetBytes(CpuID);

                byte[] md5data = md5.ComputeHash(data); md5.Clear();
                CpuID = "";
                for (int i = 0; i < md5data.Length - 1; i++)
                { CpuID += md5data[i].ToString("x").PadLeft(2, '0'); }




                Random rm = new Random();
                int a = rm.Next(0, 9);
                int b = rm.Next(0, 9);
                int c = rm.Next(0, 9);
                int d = rm.Next(0, 9);
                textBox1.Text = a.ToString() + b.ToString() + CpuID + c.ToString() + d.ToString();


                StringBuilder newName = new StringBuilder();
                for (int i = 0; i < CpuID.Length; i++)
                {
                    if (!Char.IsLetter(CpuID[i]))
                    {
                        newName.Append(CpuID[i]);
                    }
                }
                string newN = newName.ToString();

                if (newN.IndexOf("6") > -1)
                {

                    newName.Replace("6", "m");
                }
                if (newN.IndexOf("2") > -1)
                {

                    newName.Replace("2", "a");
                }
                if (newN.IndexOf("4") > -1)
                {

                    newName.Replace("4", "e");
                }
                if (newN.IndexOf("8") > -1)
                {

                    newName.Replace("8", "c");
                }
                if (newN.IndexOf("0") > -1)
                {

                    newName.Replace("0", "p");
                }
                mima = newName.ToString() + "wgsz";
                char[] ar = mima.ToCharArray();
                Array.Reverse(ar);
                mima = new string(ar);

                break;
            }
        }

        private void ZhuCeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
            Process.GetCurrentProcess().Kill();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }

        }
        int flag = 0;
       

       
    }
}
